In this entry, I'd like to discuss an OOP technique of having a class with plugins, where the plugins specify plugins for the objects used ("has-a") by instances of the class.
This occured to me in Test-Run, where I implemented several classes which I called "Structs" (like in C) which wrapped around a hash ref using accessors. The instances of these classes were referenced by the main class ("Test::Run::Core"). I graudually moved more and more methods into the helper classes, if I saw they only referred to their own data.
Now, I have some plugins of the Test::Run::Core class. It happened to me at least once now, that after I moved a few methods to the helper classes, the plugin broke, because the method in question was no longer present in the main class. I had to find a way to extend the helper classes, and it made me thinking that I'd like to have some plugins for it too.
Plugins in Perl, at least as implemented by Catalyst and Test-Run, are done by creating an empty class (let's say MyPluggableClass), adding all the plugins, in front to @MyPluggableClass::ISA, and adding the master class which the plugins enhance at the end. So @MyPluggableClass::ISA will look something like:
( qw(Plugin::Foo Plugin::Bar Plugin::Quux MasterClass) )
Now, I'd like a way for plugins of the Test::Run main class, to add plugins to its helper classes. I already have some code to traverse the inheritance tree and collect an array from the individual methods (shamelessly borrowed from a similar paradigm in Class::Std). Using it, I can collect all the plugins, append the master class at the end, and initialise a new instance of the helper class.
I can even abstract this functionality into its own meta class. So my questions are:
I think one big monolithic class is not the way to go, so I'd appreciate any comments.
Was this done before?
Sure.
Is there a better way to do it?
I'm in the camp that believes that there's always a better way to do it.
Does Moose support a similar paradigm?
Sure.
Should I rethink my strategy?
No idea, since I don't know T:R.
Re:Answers
Shlomi Fish on 2007-06-11T17:21:09
Was this done before?Sure.
OK.
:-) Is there a better way to do it?I'm in the camp that believes that there's always a better way to do it.
OK, so I suppose that as far as consulting you, my way is good enough.
Does Moose support a similar paradigm?Sure.
Nice. In any case, I was looking for a built-in or default way for plugins of main classes to specify the plugins of the helper classes, without me having to write too much helper code. What you showed was a generic plugin mechanism for Moose, not such a way. What I want is still doable, but should still be done explicitly.
Should I rethink my strategy?No idea, since I don't know T:R.
.phaylon OK, no problem.